package com.bt.lms.ui.page.sys.role;

import com.bt.lms.entity.page.Page;
import com.bt.lms.main.BotLmsApp;
import com.bt.lms.ui.page.sys.utils.MyButton;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import com.bt.lms.ui.page.sys.utils.NetWorkUtils;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.ui.page.sys.role.action.RASaveBtnAction;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

/**
 * 新增角色界面
 */
public class PageAdd extends VBox {

    /**
     * 角色名称
     */
    public static final String NAME_TF = "nameTF";

    /**
     * 角色说明
     */
    public static final String STATE_TA = "stateTA";

    /**
     * 界面跳转信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 构建新增界面
     * @param pageSkipInfo 界面跳转信息
     */
    public PageAdd(PageSkipInfo pageSkipInfo) {

        //维护
        this.pageSkipInfo = pageSkipInfo;

        //大小
        this.setPrefWidth(BotLmsApp.PER_WIGHT);
        this.setPrefHeight(BotLmsApp.HEIGHT);

        //布局
        Insets insets = new Insets(0);
        this.setPadding(insets);

        //组件
        //定位区域
        MyHBox locationHBox = new MyHBox();
        locationHBoxInit(locationHBox);
        ObservableList<Node> locationChildren = this.getChildren();
        locationChildren.add(locationHBox);

        //输入区域
        MyVBox inputVBox = new MyVBox();
        inputVBoxInit(inputVBox);
        ObservableList<Node> inputChildren = this.getChildren();
        inputChildren.add(inputVBox);

        //控制区域
        MyHBox controlHBox = new MyHBox();
        controlHBoxInit(controlHBox);
        ObservableList<Node> controlChildren = this.getChildren();
        controlChildren.add(controlHBox);

    }

    /**
     * 输入区域初始化
     *
     * @param inputVBox 输入区域
     */
    private void inputVBoxInit(MyVBox inputVBox) {

        //布局
        Insets insets = new Insets(12);
        inputVBox.setPadding(insets);
        inputVBox.setSpacing(8);
        inputVBox.setAlignment(Pos.CENTER_LEFT);

        //组件（位置）
        Label locationLab = new Label("新增角色");
        //组件添加
        ObservableList<Node> locationChildren = inputVBox.getChildren();
        locationChildren.add(locationLab);

        //组件（输入网格
        GridPane gridPane = inputGrid();
        inputVBox.getChildren().add(gridPane);
    }

    /**
     * 网格布局
     *
     * @return 网格布局
     */
    private GridPane inputGrid() {

        //新建网格布局
        GridPane res = new GridPane();

        //大小
        res.setPrefWidth(1280);
        res.setPadding(new Insets(5));
        res.setHgap(15);//间隙（左右）
        res.setVgap(15);//间隙（上下）

        //角色编码 coding
        Label codingLab = new Label("角色编码：");
        Label codeLab = new Label("保存后即可生成");
        res.add(codingLab, 0, 1);
        res.add(codeLab, 1, 1);
        GridPane.setHalignment(codingLab, HPos.RIGHT);

        //角色名称 name
        Label nameLab = new Label("角色名称：");
        TextField nameTF = new TextField();
        nameTF.setId(NAME_TF);
        res.add(nameLab, 0, 2);
        res.add(nameTF, 1, 2);
        GridPane.setHalignment(nameLab, HPos.RIGHT);
        Label nameMustLab = new Label("*");
        nameMustLab.setTextFill(Color.RED);
        res.add(nameMustLab, 2, 2);

        //角色说明 status
        Label stateLab = new Label("角色说明：");
        TextArea stateTA = new TextArea();
        stateTA.setId(STATE_TA);
        res.add(stateLab, 0, 3);
        res.add(stateTA, 1, 3);
        GridPane.setHalignment(stateLab, HPos.RIGHT);

        return res;
    }

    /**
     * 控制区域初始化
     *
     * @param controlHBox 控制区域
     */
    private void controlHBoxInit(MyHBox controlHBox) {

        //布局
        Insets insets = new Insets(10);
        controlHBox.setPadding(insets);
        controlHBox.setSpacing(10);
        controlHBox.setAlignment(Pos.CENTER);

        //组件(保存)
        MyButton saveBtn = new MyButton("保存");
        //大小控制
        Insets saveBtnInsets = new Insets(10, 20, 10, 20);
        saveBtn.setPadding(saveBtnInsets);
        //组件添加
        ObservableList<Node> saveBtnChildren = controlHBox.getChildren();
        saveBtnChildren.add(saveBtn);
        //事件
        RASaveBtnAction RASaveBtnAction = new RASaveBtnAction(pageSkipInfo);
        saveBtn.setOnAction(RASaveBtnAction);

        //组件(取消)
        MyButton cancelBtn = new MyButton("取消");
        //大小控制
        Insets cancelBtnInsets = new Insets(10, 20, 10, 20);
        cancelBtn.setPadding(cancelBtnInsets);
        //事件
        cancelBtn.setOnAction(event -> NetWorkUtils.reqRolePagingPage(pageSkipInfo,new Page<>()));
        //组件添加
        ObservableList<Node> cancelBtnChildren = controlHBox.getChildren();
        cancelBtnChildren.add(cancelBtn);

    }


    /**
     * 定位区域初始化
     *
     * @param locationHBox 定位区域容器
     */
    private void locationHBoxInit(MyHBox locationHBox) {

        //控制数据
        int height = 42;

        //大小
        locationHBox.setPrefHeight(height);

        //布局
        Insets insets = new Insets(5);
        locationHBox.setPadding(insets);
        locationHBox.setSpacing(0);
        locationHBox.setAlignment(Pos.CENTER_LEFT);

        //组件
        Label siteLab = new Label("当前位置：");
        Label oneCatalogLab = new Label("🏠>系统管理");
        Label twoCatalogLab = new Label(">角色权限管理");
        Label threeCatalogLab = new Label(">新增角色");

        //添加组件
        locationHBox.getChildren().addAll(siteLab, oneCatalogLab, twoCatalogLab, threeCatalogLab);

    }
}
